<?php

function calculare_sec($data_v, $data_n, $format)
{
 if($data_n == "")
  {
  	$datanoua = time();
  } else {
   if($format == "")
   {
    list($data1, $timp1) = explode(" ", $data_n);
	list($ora1, $minut1, $secunda1) = explode(":", $timp1);
	list($an1, $luna1, $zi1) = explode("-", $data1);
	$datanoua = mktime($ora1, $minut1, $secunda1, $luna1, $zi1, $an1);
   } elseif($format == "sec") $datanoua = $data_n;
  }
 if($format == "")
 {
    list($data, $timp) = explode(" ", $data_v);
	list($ora, $minut, $secunda) = explode(":", $timp);
	list($an, $luna, $zi) = explode("-", $data);
	$dataveche = mktime($ora, $minut, $secunda, $luna, $zi, $an);
 } elseif($format == "sec") {
    $dataveche = $data_v;
 }	
	$diferenta = $datanoua - $dataveche;
	return $diferenta;
}
//echo calculare_sec("2007-06-13 03:30:11", "");

function verificare_data($incepere, $durata, $req)
{
 list($data1, $timp1) = explode(" ", $incepere);
 list($ora1, $minut1, $secunda1) = explode(":", $timp1);
 list($an, $luna, $zi) = explode("-", $data1);
$terminare = mktime($ora1, $minut1, $secunda1+$durata, $luna, $zi, $an);
$actual = time();

if($req == "")
{
 if($actual > $terminare)
return 0;
else 
return 1;
} else if($req == "term")
return $terminare;
}
//echo verificare_data("2007-06-13 03:30:11", "100", "");

function returnare_secunde($data, $mod) {
if ($mod == 0) {
 list($data, $timp) = explode(" ", $data);
 list($ora, $minut, $secunda) = explode(":", $timp);
 list($an, $luna, $zi) = explode("-", $data);
 $sec = mktime($ora, $minut, $secunda, $luna, $zi, $an);
  } elseif ($mod == 1) {
 list($ora, $minut, $secunda) = explode(":", $data);
 $sec = mktime($ora, $minut, $secunda);
  }
  return $sec;
}

function returnare_data($secunde, $mod) {
if ($mod == "0")
	if ($secunde >= 0)
	{
	 $secunde = round($secunde);
     $h = floor($secunde / 3600);
     $m = floor(($secunde - ($h*3600)) / 60);
     $s = $secunde - ($h*3600) - ($m*60);
	 if ($h <= 9)
	  $h = '0'.$h;  
	 if ($m <= 9)
	  $m = '0'.$m; 
	 if ($s <= 9)
	  $s = '0'.$s; 
$data = $h.":".$m.":".$s;
    }
return $data;
}

function calculare_resurse($nr_unitati, $nr_res_unitate)
{
#begin constante
$nr_sec_update = 1;
$nr_sec_interval = 3600; // 1 h
#end constante
 $rezultat = $nr_unitati*$nr_res_unitate/$nr_sec_interval*$nr_sec_update;
return $rezultat;
}
//echo calculare_resurse("100", "2.5");

function arctan($x) {
return atan($x - 12) + 10;
}

function arctan2($x) {
return atan($x - pi()) + pi()/2;
}

function crestere($n1, $duritate) {
$procent = $duritate;
$y = $n1+$n1*$procent/100*10;
 return $y;
}

function formula_incrementare($valoare, $nivel, $duritate, $mod) {
 $x = $valoare/8;
for($i=1;$i<=$nivel;$i++) {
 $x = crestere($x, $duritate);
}
 switch($mod) {
 case "2":
  return $x*arctan2($nivel/1); 
 break;
 default:
   return $x*arctan($nivel/1);
 break;
 }
}

function calculare_distanta($x1, $y1, $x2, $y2, $viteza) {
 $formula = sqrt(pow(($x2-$x1), 2)+pow(($y2-$y1), 2)); 
return $formula*$viteza;
}

function incrementare_populatie($nr_unitati,$user,$tip) {
if($tip == "inserare")
	{
	 mysql_query("UPDATE `users` SET `populatie` = `populatie` + '".$nr_unitati."' WHERE `user` = '".$user."' LIMIT 1");
	}
if($tip == "incrementare")
	{
	 mysql_query("UPDATE `users` SET `populatie` = `populatie` + '".$nr_unitati."' WHERE `user` = '".$user."' LIMIT 1");
	}
if($tip == "scadere")
	{
	 mysql_query("UPDATE `users` SET `populatie` = `populatie` - '".$nr_unitati."' WHERE `user` = '".$user."' LIMIT 1");
	}	
}

function returnare_populatie_extra($planeta) {	
	$populatie_extra = 0;
	$query1 = mysql_query("SELECT obiect, valoare FROM actiuni_locale WHERE planeta = '".$planeta."' AND tip = 'construire_unitati'");
	 while($rez_extra = mysql_fetch_array($query1)) {
	$tip_extra = $rez_extra["obiect"];
	$cost_extra = valoare_mysql("SELECT cost_populatie FROM c_unitati WHERE id = '".$tip_extra."'");
	$nr_extra = $rez_extra["valoare"];
	$populatie_extra += $nr_extra*$cost_extra; 
	 }
 return $populatie_extra;
}

function returnare_populatie($planeta) {
    $populatie = 0;
	$query2 = mysql_query("SELECT nr, tip FROM unitati WHERE planeta = '$planeta'");	
	 while ($rez_unitati = mysql_fetch_array($query2)) {
 	$tip = $rez_unitati["tip"];
 	$cost = valoare_mysql("SELECT cost_populatie FROM c_unitati WHERE id = '".$tip."'");
	$nr = $rez_unitati["nr"];
 	$populatie += $nr*$cost; 
	}
 return $populatie;
}

?>